<?php

define('IN_ECS', true);

require(dirname(__FILE__) . '/includes/init.php');
$exc = new exchange($ecs->table("marketing"), $db, 'm_id', 'm_name');

/*------------------------------------------------------ */
//-- 包装列表
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'list')
{
    assign_query_info();
    $smarty->assign('ur_here',     "营销员列表");
    $smarty->assign('action_link', array('text' => "添加营销员", 'href' => 'rg_marketing.php?act=add'));
    $smarty->assign('full_page',   1);

    $marketing_list = marketing_list();

    $smarty->assign('marketing_list',    $marketing_list['marketing_list']);
    $smarty->assign('filter',       $marketing_list['filter']);
    $smarty->assign('record_count', $marketing_list['record_count']);
    $smarty->assign('page_count',   $marketing_list['page_count']);

    $smarty->display('rg_marketing_list.htm');
}

/*------------------------------------------------------ */
//-- ajax列表
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'query')
{
    $marketing_list = marketing_list();
    $smarty->assign('marketing_list', $marketing_list['marketing_list']);
    $smarty->assign('filter',       $marketing_list['filter']);
    $smarty->assign('record_count', $marketing_list['record_count']);
    $smarty->assign('page_count',   $marketing_list['page_count']);

	$sort_flag  = sort_flag($marketing_list['filter']);
    $smarty->assign($sort_flag['tag'], $sort_flag['img']);

    make_json_result($smarty->fetch('rg_marketing_list.htm'), '', array('filter' => $marketing_list['filter'], 'page_count' => $marketing_list['page_count']));
}
/*------------------------------------------------------ */
//-- 删除
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'remove')
{
    /* 检查权限 */
    check_authz_json('marketing_manage');

    $marketing_id = empty($_REQUEST['id']) ? 0 : intval($_REQUEST['id']);
	$marketing = $db->getRow("SELECT * FROM ".$ecs->table("marketing")." WHERE m_id = '".$marketing_id."'");
    if ($exc->drop($marketing_id)){
		$db->query("DELETE FROM ".$ecs->table('code')." WHERE code_id = '".$marketing['id_code']."'");

        admin_log(addslashes($marketing['m_name']),'remove','marketing');

        $url = 'rg_marketing.php?act=query&' . str_replace('act=remove', '', $_SERVER['QUERY_STRING']);
        ecs_header("Location: $url\n");
        exit;
    }else{
        make_json_error($db->error());
    }
}
/*------------------------------------------------------ */
//-- 添加
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'add')
{
    /* 权限判断 */
    admin_priv('marketing_manage');

	/* 取得地区 */
    $region_list = get_regions(1,1);
    $smarty->assign('region_list', $region_list);

    $smarty->assign('ur_here',     "添加营销员");
    $smarty->assign('action_link', array('text' => "营销员列表", 'href' => 'rg_marketing.php?act=list'));
    $smarty->assign('form_action', 'insert');

    assign_query_info();
    $smarty->display('rg_marketing_info.htm');

}
elseif ($_REQUEST['act'] == 'insert')
{
    /* 权限判断 */
    admin_priv('marketing_manage');
	$is_code = $db->getOne("SELECT count(*) FROM ".$ecs->table("code")." WHERE code_name = '".$_POST['code_name']."'");
	if($is_code) sys_msg(sprintf("优惠码 %s 已经存在", stripslashes($_POST['code_name'])), 1);
	$is_only = $exc->is_only('m_name', $_POST['m_name']);
	if (!$is_only) sys_msg(sprintf("营销员 %s 已经存在", stripslashes($_POST['m_name'])), 1);

	$db->query("INSERT INTO ".$ecs->table('code')." (code_name, code_fee, type_id) VALUES ('$_POST[code_name]', '$_POST[code_fee]', '1')");
	$new_code_id = $db->insert_id();
	if($new_code_id){
		$db->query("INSERT INTO ".$ecs->table('marketing')." (id_code, id_province, id_city, id_dist, id_store, m_name, m_idcard, m_address, m_moblie) VALUES ('$new_code_id', '$_POST[id_province]', '$_POST[id_city]', '$_POST[id_dist]', '$_POST[id_store]', '$_POST[m_name]', '$_POST[m_idcard]', '$_POST[m_address]', '$_POST[m_moblie]')");
	}
    admin_log($_POST['m_name'],'add','marketing');

    /*添加链接*/
    $link[0]['text'] = '继续添加新营销员';
    $link[0]['href'] = 'rg_marketing.php?act=add';

    $link[1]['text'] = '返回营销员列表';
    $link[1]['href'] = 'rg_marketing.php?act=list';

    sys_msg($_POST['m_name'].'已成功添加',0, $link);
}
elseif ($_REQUEST['act'] == 'api_area'){
	$region_list = $db->getAll("SELECT * FROM ".$ecs->table('region')." WHERE parent_id='".$_REQUEST["id"]."'");
	echo json_encode($region_list);exit;
}
elseif ($_REQUEST['act'] == 'storebydist'){
	$store_list = $db->getAll("SELECT id, name FROM ".$ecs->table('store')." WHERE id_dist='".$_REQUEST["id_dist"]."'");
	echo json_encode($store_list);exit;
}
/*------------------------------------------------------ */
//-- 编辑
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'edit')
{
    /* 权限判断 */
    admin_priv('marketing_manage');

	/* 取得地区 */
    $region_list = get_regions(1,1);
    $smarty->assign('region_list', $region_list);

    $marketing = $db->GetRow("SELECT * FROM " .$ecs->table('marketing'). " WHERE m_id='$_REQUEST[id]'");
	if($marketing){
		$marketing['code_name'] = $db->GetOne("SELECT code_name FROM " .$ecs->table('code'). " WHERE code_id='$marketing[id_code]'");
		$marketing['code_fee'] = $db->GetOne("SELECT code_fee FROM " .$ecs->table('code'). " WHERE code_id='$marketing[id_code]'");
	}

    $smarty->assign('ur_here',     '编辑营销员');
    $smarty->assign('action_link', array('text' => '营销员列表', 'href' => 'rg_marketing.php?act=list&' . list_link_postfix()));
    $smarty->assign('marketing',        $marketing);
    $smarty->assign('form_action', 'update');

    assign_query_info();
    $smarty->display('rg_marketing_info.htm');
}
elseif ($_REQUEST['act'] == 'update')
{
    /* 权限判断 */
    admin_priv('marketing_manage');
	
    if ($_POST['m_name'] != $_POST['old_mname']){
        $is_only = $exc->is_only('m_name', $_POST['m_name'], $_POST['id']);
        if (!$is_only) sys_msg(sprintf("营销员 %s 已经存在", stripslashes($_POST['m_name'])), 1); 
    }
    $param = "id_province = '$_POST[id_province]', id_city = '$_POST[id_city]', id_dist = '$_POST[id_dist]', id_store = '$_POST[id_store]', m_name = '$_POST[m_name]', m_idcard = '$_POST[m_idcard]', m_address = '$_POST[m_address]', m_moblie = '$_POST[m_moblie]'";

    if ($exc->edit($param,  $_POST['id'])){
        admin_log($_POST['m_name'], 'edit', 'marketing');

        $link[0]['text'] = '返回列表';
        $link[0]['href'] = 'rg_marketing.php?act=list&' . list_link_postfix();

        $note = sprintf('营销员 %s 修改成功', $_POST['m_name']);
        sys_msg($note, 0, $link);
    }else{
        die($db->error());
    }
}
/*------------------------------------------------------ */
//-- ajax编辑名字
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'edit_m_name')
{
    check_authz_json('marketing_manage');
    $m_id = empty($_REQUEST['id']) ? 0 : intval($_REQUEST['id']);
    $m_name = empty($_REQUEST['val']) ? '' : json_str_iconv(trim($_REQUEST['val']));

    if (!$exc->is_only('m_name', $m_name, $m_id)) make_json_error(sprintf("营销员 %s 已经存在", $m_name)); 
    $old_m_name = $exc->get_name($m_id);
    if ($exc->edit("m_name='$m_name'", $m_id)){
        admin_log(addslashes($old_m_name), 'edit', 'marketing');
        make_json_result(stripcslashes($m_name));
    }else{
        make_json_error($db->error());
    }
}

function marketing_list()
{
    $result = get_filter();
    if ($result === false){
        $filter['sort_by']    = empty($_REQUEST['sort_by']) ? 'm_id' : trim($_REQUEST['sort_by']);
        $filter['sort_order'] = empty($_REQUEST['sort_order']) ? 'DESC' : trim($_REQUEST['sort_order']);

        /* 分页大小 */
        $sql = "SELECT count(*) FROM " .$GLOBALS['ecs']->table('marketing');
        $filter['record_count'] = $GLOBALS['db']->getOne($sql);

        $filter = page_and_size($filter);

        /* 查询 */
        $sql = "SELECT * FROM ".$GLOBALS['ecs']->table('marketing').
               " ORDER by " . $filter['sort_by'] . ' ' . $filter['sort_order'] .
               " LIMIT " . $filter['start'] . ',' . $filter['page_size'];

        set_filter($filter, $sql);
    }else{
        $sql    = $result['sql'];
        $filter = $result['filter'];
    }

    $marketing_list = $GLOBALS['db']->getAll($sql);
	
	foreach($marketing_list as $_k=>$_v){
		$id_province = $GLOBALS['db']->getOne("SELECT region_name FROM " .$GLOBALS['ecs']->table('region')." WHERE region_id = '".$_v['id_province']."'");
		$id_city = $GLOBALS['db']->getOne("SELECT region_name FROM " .$GLOBALS['ecs']->table('region')." WHERE region_id = '".$_v['id_city']."'");
		$id_dist = $GLOBALS['db']->getOne("SELECT region_name FROM " .$GLOBALS['ecs']->table('region')." WHERE region_id = '".$_v['id_dist']."'");

		$marketing_list[$_k]['code_name'] = $GLOBALS['db']->getOne("SELECT code_name FROM " .$GLOBALS['ecs']->table('code')." WHERE code_id = '".$_v['id_code']."'");
		$marketing_list[$_k]['area_name'] = $id_province.$id_city.$id_dist;
		$marketing_list[$_k]['store_name'] = $GLOBALS['db']->getOne("SELECT name FROM " .$GLOBALS['ecs']->table('store')." WHERE id = '".$_v['id_store']."'");
	}

    $arr = array('marketing_list' => $marketing_list, 'filter' => $filter, 'page_count' => $filter['page_count'], 'record_count' => $filter['record_count']);

    return $arr;
}

?>